package vn.cuhanh.fragment;

import java.util.ArrayList;

import vn.cuhanh.BaseActivity;
import vn.cuhanh.ChooseGameActivity;
import vn.cuhanh.GameDetailActivity;
import vn.cuhanh.NewsDetailActivity;
import vn.cuhanh.R;
import vn.cuhanh.bean.PlayType;
import vn.cuhanh.bean.ShareInfo;
import vn.cuhanh.utils.HelperFunc;
import vn.cuhanh.utils.ShareZingMe;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class PlayTypeFragment extends BasicFragment {
	
	private ArrayList<PlayType> playTypes;
	PlayTypeGridAdapter adapter;
	public final int ITEM_PER_PAGE = 10;
	public boolean haveMoreData = true;
	public int currentPage = 0;
	private View view;

	public static PlayTypeFragment newInstance(Context ctx) {
		return new PlayTypeFragment(ctx);
	}

	public PlayTypeFragment(Context ctx) {
		super(ctx);
	}

	public PlayTypeFragment() {
		super(GameDetailActivity.gameDetailContext);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onPause() {
		super.onPause();
		// HelperFunc.unbindDrawables(view);
		// System.gc();
	}

	@Override
	public void onDestroy() {

		super.onDestroy();
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(ChooseGameActivity.current == null) {
			Log.d("PlayType", "ChooseGameActivity.current is null");
		} else {
			Log.d("PlayType", "Not null");
		}
		playTypes = HelperFunc.getPlayTypes(ctx, ChooseGameActivity.current.getId());
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		try {
			view = inflater.inflate(R.layout.play_types, null);
			if (ctx == null || view == null) {
				if (isAdded()) {
					ctx = getActivity();
				} else {
					return view;
				}
			}
			GridView playTypesGrid = (GridView) view.findViewById(R.id.play_types_grid);
			adapter = new PlayTypeGridAdapter(ctx, R.id.play_types_grid);
			playTypesGrid.setAdapter(adapter);
			return view;
		} catch (InflateException e) {
			HelperFunc.readStackTrace(e);
		} catch (Exception e) {
			HelperFunc.readStackTrace(e);
		}
		return null;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		BaseActivity.updateDndScore(view);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setRetainInstance(true);
	}
	
	public class PlayTypeGridAdapter extends ArrayAdapter<PlayType> {

		public PlayTypeGridAdapter(Context context, int textViewResourceId) {
			super(context, textViewResourceId);
			// TODO Auto-generated constructor stub
		}
		
		@Override
		public int getCount() {
			return playTypes.size();
		}
		
		@Override
		public View getView(int position, View v, ViewGroup parent) {
			if(v == null) {
				LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.play_type_grid_item, null);
			}
			final PlayType item = playTypes.get(position);
			if(item == null) {
				return v;
			}
			ImageView image = (ImageView)v.findViewById(R.id.play_type_grid_item_image);
			image.setImageResource(ctx.getResources().getIdentifier(item.getImage(), "drawable", ctx.getPackageName()));
			
			v.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					HelperFunc.trackEvent(ctx, "play_type_fragment", "play_type_" + item.getId() + "_press", null);
					Intent intent = new Intent(ctx, NewsDetailActivity.class);
					intent.putExtra("type", NewsDetailActivity.TYPE_ASSET);
					intent.putExtra("content", "play_types/" + item.getDescriptionFileName());
					intent.putExtra("haveShareBtn", true);
					NewsDetailActivity.shareInfo = new ShareInfo(
							item.getDisplayName(), 
							ctx.getResources().getString(R.string.play_type_share_title), 
							ctx.getResources().getString(R.string.play_type_share_description)
								.replace("play_type_name", item.getDisplayName())
								.replace("funny_phrase", ShareZingMe.getRandomFunnyPhrase(ctx)), 
							ctx.getResources().getString(R.string.play_type_share_imgurl), 
							ctx.getResources().getString(R.string.play_type_share_local_img), 
							ctx.getResources().getString(R.string.play_type_share_link), 
							ctx.getResources().getString(R.string.play_type_share_state),
							ctx.getResources().getInteger(ctx.getResources().getIdentifier("actionIdSharePlayType" + item.getId(), "integer", ctx.getPackageName())));
					startActivity(intent);
//					Intent intent = new Intent(ctx, LoginFacebookActivity.class);
//					startActivity(intent);
				}
			});
//			ShareZingMe.getRandomFunnyPhrase(ctx);
			return v;
		}
		
	}
}
